package hanelsoft.vn.dictionaylearn.model.common;

import hanelsoft.vn.dictionaylearn.R;
import hanelsoft.vn.dictionaylearn.model.db.DictionaryEngine;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

public class VocadictProvider extends ContentProvider {

	public static final String PROVIDER_TAG = "[VocadictProvider]";
	public static final String PROVIDER_NAME = "hanelsoft.vn.dictionaylearn.model.common.VocadictProvider";
	public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME);
	
	public static final String _ID = "id";
	public static final String COLUMN_WORD = "Word";
	public static final String COLUMN_CONTENT = "Content";

	private static final int CODE_LIST_EMPTY = 0;
	private static final int CODE_LIST = 1;
	private static final int CODE_CONTENT_FROM_ID = 2;
	private static final int CODE_CONTENT_FROM_WORD = 3;
	
	private String mDBExtension;
	private String mDBPath;
	private String mCurrentDB = null;
	
	private static final UriMatcher uriMatcher;
	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(PROVIDER_NAME, "dict/*/list/", CODE_LIST_EMPTY);
		uriMatcher.addURI(PROVIDER_NAME, "dict/*/list/*", CODE_LIST);
		uriMatcher.addURI(PROVIDER_NAME, "dict/*/contentId/#", CODE_CONTENT_FROM_ID);
		uriMatcher.addURI(PROVIDER_NAME, "dict/*/contentWord/*", CODE_CONTENT_FROM_WORD);
	}

	private DictionaryEngine mDBEngine;

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		switch (uriMatcher.match(uri))
		{
			case CODE_LIST:
			case CODE_LIST_EMPTY:
			case CODE_CONTENT_FROM_WORD:
			case CODE_CONTENT_FROM_ID:
	        default:
	            throw new IllegalArgumentException("VocadictProvider - Unsupported URI: " + uri);        
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		return null;
	}

	@Override
	public boolean onCreate() {
        mDBExtension = getContext().getResources().getString(R.string.dbExtension);
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        	mDBPath = getContext().getResources().getString(R.string.dbPath);
        }
        else
        {
        	mDBPath = getContext().getResources().getString(R.string.dbPathNotSDCard);
        }
        mDBEngine = new DictionaryEngine();
        return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		String strDB = uri.getPathSegments().get(1);

		if (mCurrentDB == null || !mCurrentDB.equals(strDB))
		{
			mCurrentDB = strDB;
			mDBEngine.setDatabaseFile(mDBPath, strDB, mDBExtension);
		}
		if (mDBEngine == null)
		{
        	Log.e(PROVIDER_TAG,"Can not create database engine");
			return null;
		}
		
		String word;
		int wordId;
		Cursor c;
		switch (uriMatcher.match(uri))
		{
			case CODE_LIST_EMPTY:
				c = mDBEngine.getCursorWordList("");
				c.setNotificationUri(getContext().getContentResolver(), uri);
				return c;
			case CODE_LIST:
				word = uri.getPathSegments().get(3);
				c = mDBEngine.getCursorWordList(word);
				c.setNotificationUri(getContext().getContentResolver(), uri);
				return c;
			case CODE_CONTENT_FROM_ID:
				wordId = Integer.parseInt(uri.getPathSegments().get(3));
				c = mDBEngine.getCursorContentFromId(wordId);
				c.setNotificationUri(getContext().getContentResolver(), uri);
				return c;
				
			case CODE_CONTENT_FROM_WORD:
				word = uri.getPathSegments().get(3);
				c = mDBEngine.getCursorContentFromWord(word);
				c.setNotificationUri(getContext().getContentResolver(), uri);
				return c;
				
	        default:
	        	return null;
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		return 0;
	}

}
